package TestFiles;
import graphdisplay.AbstractGraph;
import graphdisplay.Displayable;
import graphdisplay.Graph;
import graphdisplay.WeightedEdge;
import graphdisplay.WeightedGraph;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;



public class GuiForShortestPath extends JFrame implements MouseListener,MouseMotionListener{
	  private JTextField jtfVertexName = new JTextField(5);
	  private JTextField jtfX = new JTextField(5);
	  private JTextField jtfY = new JTextField(5);
	  private JButton jbtAddVertex = new JButton("Add Vertex");
	  
	  private JTextField jtfu = new JTextField(5);
	  private JTextField jtfv = new JTextField(5);
	  private JTextField jtfWeight = new JTextField(5);
	  private JButton jbtAddEdge = new JButton("Add Edge");

	  
	  private JTextField shrstart= new JTextField(5);
	  private JTextField shrend = new JTextField(5);
	  private JButton shrpath = new JButton("Shortest Path");

	  private JButton importing = new JButton("Import");
	  private JButton export = new JButton("Export");
	  public JLabel c1= new JLabel("File Path");

	public static  List<Vertex> verticeslist = new ArrayList<Vertex>();
	  public static List<Vertex> verticeslistwithedges = new ArrayList<Vertex>();
	  public static List<Vertex> verticeslistwithoutedges = new ArrayList<Vertex>();
	  public static List<WeightedEdge> edgeslist = new ArrayList<WeightedEdge>();
	  
	  boolean flag=false;
	  
	int z;
	  
	  JScrollPane js;

	  private WeightedGraph<Vertex> graph; //= new WeightedGraph<Vertex>(edges,vertices);
	  private GraphView view; //= new GraphView(graph);
	  
	  
	  
	/**
	 * Create the applet.
	 */
	public GuiForShortestPath() {
		
		
		JPanel panel1 = new JPanel(new GridLayout(4, 2));
	    panel1.add(new JLabel("Vertex name: "));
	    panel1.add(jtfVertexName);
	    panel1.add(new JLabel("x-coordinate: "));
	    panel1.add(jtfX);
	    panel1.add(new JLabel("y-coordinate: "));
	    jtfY.setToolTipText("-");
	    panel1.add(jtfY);
	    panel1.add(new JLabel());
	    panel1.add(jbtAddVertex);
	    panel1.setBorder(new TitledBorder("Add a new vertex"));
	    
	    JPanel panel2 = new JPanel(new GridLayout(4, 2));
	    panel2.add(new JLabel("Vertex u (index): "));
	    panel2.add(jtfu);
	    panel2.add(new JLabel("Vertex v (index): "));
	    panel2.add(jtfv);
	    panel2.add(new JLabel("Weight (int): "));
	    panel2.add(jtfWeight);
	    panel2.add(new JLabel(""));
	    panel2.add(jbtAddEdge);
	    panel2.setBorder(new TitledBorder("Add a new edge"));
	    

	    
	    
	    JPanel panelShr = new JPanel(new GridLayout(3, 2));
	    panelShr.add(new JLabel("Starting Vertex (index): "));
	    shrstart.setToolTipText("Enter Integers which is present in  graph ");
	    panelShr.add(shrstart);
	    panelShr.add(new JLabel("Ending Vertex (index): "));
	    shrend.setToolTipText("Enter Integers which is present in  graph ");
	    panelShr.add(shrend);
	    panelShr.add(new JLabel(""));
	    panelShr.add(shrpath);
	    panelShr.setBorder(new TitledBorder("Find a Shortest Path"));
	    
	    JPanel panelimp = new JPanel(new GridLayout(2, 2));
	    panelimp.add(importing);
	    panelimp.add(export);
	    panelimp.add(c1);
	    panelimp.setBorder(new TitledBorder("Import/Export"));
	  
	    
	    
	    
	    JPanel panel4 = new JPanel(new GridLayout(1, 2));
	    panel4.add(panel1);
	    panel4.add(panel2);
	    panel4.add(panelShr);
	    panel4.add(panelimp);
	    
	    JPanel panel5 = new JPanel(new BorderLayout());
	    panel5.add(panel4);
	  
	    
	    verticeslist.add(new Vertex("0 Seattle", 75, 50,0));
	    verticeslist.add(new Vertex("1 San Francisco", 50, 210,1));
	    verticeslist.add(new Vertex("2 Los Angeles", 75, 275,2));
	    verticeslist.add(new Vertex("3 Denver", 275, 175,3));
	    verticeslist.add(new Vertex("4 Kansas City", 400, 245,4));
	    verticeslist.add(new Vertex("5 Chicago", 450, 100,5));
	    verticeslist.add(new Vertex("6 Boston", 700, 80,6));
	    verticeslist.add(new Vertex("7 New York", 675, 120,7));
	    verticeslist.add(new Vertex("8 Atlanta", 575, 295,8));
	    verticeslist.add(new Vertex("9 Miami", 600, 400,9));
	    verticeslist.add(new Vertex("10 Dallas", 408, 325,10));
	    verticeslist.add(new Vertex("11 Houston", 450, 360,11));
	    
	    

	  
	    // Edge array for graph in Figure 27.1
	    edgeslist.add(new WeightedEdge(0, 1,807));
	    edgeslist.add(new WeightedEdge(0, 3,1331));
	    edgeslist.add(new WeightedEdge(0, 5,2097));

	    edgeslist.add(new WeightedEdge(1, 0,807));
	    edgeslist.add(new WeightedEdge(1, 2,381));
	    edgeslist.add(new WeightedEdge(1, 3,1267));

	    edgeslist.add(new WeightedEdge(2, 1,381));
	    edgeslist.add(new WeightedEdge(2, 3,1015));
	    edgeslist.add(new WeightedEdge(2, 4,1663));
	    edgeslist.add(new WeightedEdge(2, 10,1435));

	    edgeslist.add(new WeightedEdge(3, 0,1331));
	    edgeslist.add(new WeightedEdge(3, 1,1267));
	    edgeslist.add(new WeightedEdge(3, 2,1015));
	    edgeslist.add(new WeightedEdge(3, 4,599));
	    edgeslist.add(new WeightedEdge(3, 5,1003));

	    edgeslist.add(new WeightedEdge(4, 2,1663));
	    edgeslist.add(new WeightedEdge(4, 3,599));
	    edgeslist.add(new WeightedEdge(4, 5,533));
	    edgeslist.add(new WeightedEdge(4, 7,1260));
	    edgeslist.add(new WeightedEdge(4, 8,864));
	    edgeslist.add(new WeightedEdge(4, 10,496));

	    edgeslist.add(new WeightedEdge(5, 0,2097));
	    edgeslist.add(new WeightedEdge(5, 3,1003));
	    edgeslist.add(new WeightedEdge(5, 4,533));
	    edgeslist.add(new WeightedEdge(5, 6,983));
	    edgeslist.add(new WeightedEdge(5, 7,787));

	    edgeslist.add(new WeightedEdge(6, 5,983));
	    edgeslist.add(new WeightedEdge(6, 7,214));

	    edgeslist.add(new WeightedEdge(7, 4,1260));
	    edgeslist.add(new WeightedEdge(7, 5,787));
	    edgeslist.add(new WeightedEdge(7, 6,214));
	    edgeslist.add(new WeightedEdge(7, 8,888));

	    edgeslist.add(new WeightedEdge(8, 4,864));
	    edgeslist.add(new WeightedEdge(8, 7,888));
	    edgeslist.add(new WeightedEdge(8, 9,661));
	    edgeslist.add(new WeightedEdge(8, 10,781));
	    edgeslist.add(new WeightedEdge(8, 11,810));

	    edgeslist.add(new WeightedEdge(9, 8,661));
	    edgeslist.add(new WeightedEdge(9, 11,1187));

	    edgeslist.add(new WeightedEdge(10, 2,1435));
	    edgeslist.add(new WeightedEdge(10, 4,496));
	    edgeslist.add(new WeightedEdge(10, 8,781));
	    edgeslist.add(new WeightedEdge(10, 11,239));

	    edgeslist.add(new WeightedEdge(11, 8,810));
	    edgeslist.add(new WeightedEdge(11, 9,1187));
	    edgeslist.add(new WeightedEdge(11, 10,239));
	    verticeslistwithedges.addAll(verticeslist);
	    graph = new WeightedGraph<Vertex>(edgeslist,verticeslist);
	    view = new GraphView(graph);
	    js=new JScrollPane(view);
	  
	    	   
	    getContentPane().add(js); 	    
	    getContentPane().add(panel5, BorderLayout.SOUTH);
	    js.addMouseListener(this);
	    js.addMouseMotionListener(this);
	    jbtAddVertex.addActionListener(new ActionListener() {
	      public void actionPerformed(ActionEvent e) {
	        String name = jtfVertexName.getText();
	        int x=0,y=0;    
try{	        
	        	 if( jtfX.getText().trim().length()>0){
		         x = Integer.parseInt(jtfX.getText().trim());
	        		 
	        	 }	 
	        	else
		    		 JOptionPane.showMessageDialog(
			   	              null, "Node data to add edge is incomplete or undefined  ");
		        
		    	 if(jtfY.getText().trim().length()>0){
		    	 y = Integer.parseInt(jtfY.getText().trim());
		    	 }else
		        	JOptionPane.showMessageDialog(
			   	              null, "Node data to add edge is incomplete or undefined  ");
}catch (Exception oarsing) {
	// TODO: handle exception
	JOptionPane.showMessageDialog(
	              null, "Enter Valid Data i.e,integers  ");
}	    	 
	           if(x>getSize().getWidth()  )
	           {
		        	JOptionPane.showMessageDialog(
			   	              null, "Width must be less than "+getSize().getWidth());
	           }
	           else if (y>getSize().getHeight()) {
	        	   JOptionPane.showMessageDialog(
			   	              null, "Height must be less than "+getSize().getHeight());
			   }
	           else
	           { 	           
	        	   verticeslistwithoutedges.add(new Vertex(verticeslist.size()+ " "  +name, x, y,verticeslist.size()));

	        	   graph.addVertex(new Vertex(verticeslist.size()+ " "  +name, x, y,verticeslist.size()));
	             
	           }
	          view.repaint();
	         
	      }
	    }
	    );   
	    
	    jbtAddEdge.addActionListener(new ActionListener() {
	      public void actionPerformed(ActionEvent e) {
	    	  int u=0;
	    	  int v=0;
	    	  double weight = 0;

	    	  try{
	    	 if( jtfu.getText().trim().length()>0){
	         u = Integer.parseInt(jtfu.getText().trim());
	    	 }
	    	 else
	    		 JOptionPane.showMessageDialog(
		   	              null, "please provide an edge Vertex U ");
	        
	    	 if(jtfv.getText().trim().length()>0)
	    	 v = Integer.parseInt(jtfv.getText().trim());
	         else
	        	JOptionPane.showMessageDialog(
		   	              null, "please provide an edge Vertex V ");
	        
	        if(jtfWeight.getText().trim().length()<=0)
	        {
	        	 JOptionPane.showMessageDialog(
	   	              null, "please provide an edge weight ");
	        }else
	        weight = Double.parseDouble(jtfWeight.getText().trim());

	    	  }catch (Exception edgeParsing) {
				// TODO: handle exception
	    		  
	    		  JOptionPane.showMessageDialog(
		   	              null, "please Enter valid integers ");
			}
	        
	        
	        if ((u < 0 || u >= graph.getSize()) )
	          JOptionPane.showMessageDialog(
	            null, "Vertex " + u + " is not in the graph"+"it must be less than or equal to "+(graph.getSize()-1));
	        else if ((v < 0 || v >= graph.getSize()) )
	          JOptionPane.showMessageDialog(
	            null, "Vertex " + v + " is not in the graph"+"it must be less than or equal to "+(graph.getSize()-1));
	        else if (u == v) 
	          JOptionPane.showMessageDialog(
	              null, "Two vertices cannot be the same");
	        else {
	        	//graph.RemoveEdge(u,v);
	        	   view.repaint();
	        	   if(weight<0 || jtfWeight.getText().trim().length()<=0 ){
	        		   /*JOptionPane.showMessageDialog(
	        		            null, "Please provide an edge weight...");*/
	        		   }
	        	   else
	            //  graph.addEdge(u, v, (int) weight);
	          
	             System.out.print("before no of edges = " + edgeslist.size());
	             
	            
	             
	             
	             for(int i=0; i<edgeslist.size(); ++i)
	             {
	            	 if(edgeslist.get(i).u ==u && edgeslist.get(i).v==v)
	            		edgeslist.remove(i); 
	             }
	             
	             for(int i=0; i<edgeslist.size(); ++i)
	             {
	            	 if(edgeslist.get(i).u ==v && edgeslist.get(i).v==u)
	            		edgeslist.remove(i); 
	             }
	            System.out.print(" removed no of edges = " + edgeslist.size());
	   	         
	   	         //  view.repaint();
	             edgeslist.add(new WeightedEdge(u, v, (int) weight));
	             edgeslist.add(new WeightedEdge(v, u, (int) weight));
	             System.out.print("added verticeslistwithoutedges= " + verticeslistwithoutedges.size());
	             for(int k=0;k<verticeslistwithoutedges.size();++k)
	             {
	            	 if(verticeslistwithoutedges.get(k).NodeNo==u ||verticeslistwithoutedges.get(k).NodeNo==v) 
	            	 {
	            	 //vertices found for which edge is created
	            		 verticeslistwithedges.add(verticeslistwithoutedges.get(k));
	            		 System.out.print("adding vertex with edge list"+verticeslistwithedges.size());
	            		 verticeslistwithoutedges.remove(k);
	            	 }
	             }
	            view.repaint();
	        }
	      }
	    });   
	    shrpath.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent e) {
	        	int u=0,v=0;
	        	//for checking u is empty or not
	        	try{ 
	        	if( shrstart.getText().trim().length()>0){
	        		 u = Integer.parseInt(shrstart.getText().trim());
	        	 }
	    	    	 else
	    	    		 JOptionPane.showMessageDialog(
	    		   	              null, "Two already defined nodes must be used when calculating shortest path.");
	        	 if( shrend.getText().trim().length()>0){
	        		  v= Integer.parseInt(shrend.getText().trim());
	        	 }
	    	    	 else
	    	    		 JOptionPane.showMessageDialog(
	    		   	              null, "Two already defined nodes must be used when calculating shortest path.");
	        	}catch (Exception weightparsing) {
					// TODO: handle exception
	        		JOptionPane.showMessageDialog(
  		   	              null, "Enter valid datai.e.integers");}
	      
	        try {
	        	  
	        	  if ((u < 0 || u >= graph.getSize()) )
	    	          JOptionPane.showMessageDialog(
	    	            null, "Vertex " + u + " is not in the graph"+"it must be less than or equal to "+(graph.getSize()-1));
	    	        else if ((v < 0 || v >= graph.getSize()) )
	    	          JOptionPane.showMessageDialog(
	    	            null, "Vertex " + v + " is not in the graph"+"it must be less than or equal to "+(graph.getSize()-1));
	    	        else if (u == v) 
	    	          JOptionPane.showMessageDialog(
	    	              null, "Two vertices cannot be the same for finding the shortest path");
	        	  try
	  	        	{ 
	        		  System.out.print(" 1 try shrpath"+"u="+u+"V="+v+"vert size:"+verticeslistwithedges.size());
	        		  if((!verticeslistwithoutedges.contains(verticeslist.get(u)))&&(!verticeslistwithoutedges.contains(verticeslist.get(v)))){
	        			  
	        			  WeightedGraph<String> graph1 = 
	    	    	        	  new WeightedGraph<String>(edgeslist, verticeslistwithedges.size());
	    	        	  
	    	        			System.out.print("2 try shrpath"+"u="+u+"V="+v+"vert size:"+verticeslistwithedges.size());
	    	    	        	  WeightedGraph<String>.ShortestPathTree tree1 = 
	    	    	   			  graph1.getShortestPath(v);
	    	    	        	  System.out.print("3 try shrpath");
	    	    	        	  java.util.List<Integer> path = tree1.getPath1(u);
	    	    	        	  System.out.print("4try shrpath");
	    	    	        	   view.setTree1(true,path,verticeslist);//drawing
	    	    	        	   System.out.print("5 try shrpath");
	        			 
		        	  }
	        		  else{
	        			  JOptionPane.showMessageDialog(
	   	   	    	              null, "There does not exist a path between the two nodes selected.: ");
	        		  }
	        	  
	        			  
	        		  
	    	        	
	    	        		
	    	        	}
	    	        	catch (Exception excep) {
							// TODO: handle exception
	    	        		 JOptionPane.showMessageDialog(
	   	   	    	              null, "There does not exist a path between the two nodes selected.: ");
	    	        		 System.out.println(excep);
						}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	          view.repaint();
	        }
	        
	      }); 
	    importing.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent e) {
	         String strline;
	        	 JFileChooser chooser = new JFileChooser();
				    
				    try {
				    	chooser.showOpenDialog(importing );
				        
				    	File field = chooser.getSelectedFile();
				    
				        
				        
			               c1.setText(field.getAbsolutePath());
			               File f=new File(new File("filename.txt").getCanonicalPath());
			               chooser.setSelectedFile(f);
			               java.io.FileInputStream input=new FileInputStream(field);
			            DataInputStream ins=new DataInputStream(input);
			            BufferedReader b=new BufferedReader(new InputStreamReader(ins));
			            StringBuffer sb=new StringBuffer();
					    while((strline=b.readLine())!=null){
					    	//System.out.println(strline);
					    	sb.append(strline+"\n");
					    	
					    }
					    String string=sb.toString();
					    StringTokenizer s=new StringTokenizer(string, ":.,\n");
					    //System.out.println(s.countTokens());
					    verticeslist.clear();
					    verticeslistwithedges.clear();
					    verticeslistwithoutedges.clear();
					    edgeslist.clear();
					     while(s.hasMoreTokens()){
					    	int z,y;
					    	
					    	if(s.nextToken().equals("no of vertices present in the graph are")){
					    		
					            z=Integer.parseInt(s.nextToken());
					            String s1=s.nextToken();
					        
					          try{
					          for(int i=0;i<z;i++){
					        	  String a=new String(s.nextToken());
					        	  int a1=Integer.parseInt(s.nextToken());
					        	 int a2=Integer.parseInt(s.nextToken());
					        	 int a3=Integer.parseInt(s.nextToken());
					        	 //System.out.println("width "+getSize().getWidth());
					        	 
					        	 if((a1<getSize().getWidth())&& (a2<getSize().getHeight())){
					        	 Vertex v=new Vertex(a,a1,a2,a3);
					        	 verticeslist.add(i, v);
					        	 }else{
					        		 JOptionPane.showMessageDialog(
							   	              null, "width and hieght should be in prescribed format");
					        	 }
					        	 
					        	  
					        	  
					        	 
					        	  
					          }
					          }catch (Exception verticesParsing) {
								// TODO: handle exception
					        	  JOptionPane.showMessageDialog(
						   	              null, "Vertices format is incorrect:1");
					        
							}
					          }
					          
						          if(s.nextToken().equals("no of vertices present in the graph with edges are")){
							    		
							            z=Integer.parseInt(s.nextToken());
							           
							          String s1=s.nextToken();
							          
							          try{
							          for(int i=0;i<z;i++){
							        	  String a=new String(s.nextToken());
							        	  int a1=Integer.parseInt(s.nextToken());
							        	 int a2=Integer.parseInt(s.nextToken());
							        	 int a3=Integer.parseInt(s.nextToken());
							        	 //System.out.println("width "+getSize().getWidth());
							        	 
							        	 if((a1<getSize().getWidth())&& (a2<getSize().getHeight())){
							        	 Vertex v=new Vertex(a,a1,a2,a3);
							        	  verticeslistwithedges.add(i, v);
							        	 }else{
							        		 JOptionPane.showMessageDialog(
									   	              null, "width and hieght should be in prescribed format");
							        	 }
							        	 
							        	  
							          }
							          }catch (Exception verticesParsing) {
										// TODO: handle exception
							        	  JOptionPane.showMessageDialog(
								   	              null, "Vertices format is incorrect:2");
							        
									}}
							          if(s.nextToken().equals("no of vertices present in the graph without edges are")){
								    		
								            z=Integer.parseInt(s.nextToken());
								            
								          String s1=s.nextToken();
								          
								          try{
								          for(int i=0;i<z;i++){
								        	 
								        	  String a=new String(s.nextToken());
								        	  int a1=Integer.parseInt(s.nextToken());
								        	 int a2=Integer.parseInt(s.nextToken());
								        	 int a3=Integer.parseInt(s.nextToken());
								        	 //System.out.println("width "+getSize().getWidth());
								        	 
								        	 if((a1<getSize().getWidth())&& (a2<getSize().getHeight())){
								        	 Vertex v=new Vertex(a,a1,a2,a3);
								        	  verticeslistwithoutedges.add(i, v);
								        	 }else{
								        		 JOptionPane.showMessageDialog(
										   	              null, "Width and Hieght should be in Prescribed Format");
								        	 }
								        	
								        	  
								          }
								          }catch (Exception verticesParsing) {
											// TODO: handle exception
								        	  JOptionPane.showMessageDialog(
									   	              null, "Vertices format is incorrect:3");
								        
										}}
					          if(s.nextToken().equals("no of edges  present in the graph are")){
						    		
						    		y=Integer.parseInt(s.nextToken());
						    		
						    		String s2=s.nextToken();
						    		
						    	try{
						    	for(int i=0;i<y;i++){
						    		
						        	  WeightedEdge w=new WeightedEdge(Integer.parseInt(s.nextToken()),Integer.parseInt(s.nextToken()),Integer.parseInt(s.nextToken()));
						        	  edgeslist.add(i, w);
						        	  
						               
						        	  
						        	
						        	  
						          }
						    	}catch (Exception importParsing) {
									// TODO: handle exception
						    		 JOptionPane.showMessageDialog(
							   	              null, "edgeeslist Format is incorrect");
						        
								}
					          }
					    	}
						          
						          
					    System.out.print("adding new graph : + "+	edgeslist.size() +"  vert = "+ verticeslistwithedges.size());
					    	
					    System.out.println("Verticeslist :"+verticeslist.size());
					    
					  
					    
					    System.out.print("\nafter adding new graph : + "+	edgeslist.size() +"  vert = "+ verticeslistwithedges.size());
					  view.repaint();
					  }
					    	   catch (IOException e1) {
					    		   JOptionPane.showMessageDialog(
							   	              null, "Import file format not recognized");
				        }
				    
				   
				    		  
	          }
	        
	        
	      });   
	    
	    export.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent e) {
	         
	        	 JFileChooser chooser = new JFileChooser();
				    
				    try {
						  
					      chooser.showSaveDialog(export);
		               File field = chooser.getSelectedFile();
		               c1.setText(field.getAbsolutePath());
				        File f = new File(new File("filename.txt").getCanonicalPath());
				        java.io.PrintWriter output = new java.io.PrintWriter(field);
		                   
		                   int numberOfVertices = verticeslist.size();
		                   output.println("no of vertices present in the graph are:"+numberOfVertices+".");
			                   output.print("Vertices are:");
			                   for(int i=0;i<verticeslist.size();i++)
			                   {
			                   output.print(verticeslist.get(i).name+ "," +verticeslist.get(i).x+ "," +verticeslist.get(i).y+","+verticeslist.get(i).NodeNo+".");
			                   output.println();
			                   }
			                   

			                   
			                   int numberOfVerticeswithEdges = verticeslistwithedges.size();
			                   output.println("no of vertices present in the graph with edges are:"+numberOfVerticeswithEdges+".");
				                   output.print("Vertices with edges are:");
				                   for(int i=0;i<verticeslistwithedges.size();i++)
				                   {
				                   output.print(verticeslistwithedges.get(i).name+ "," +verticeslistwithedges.get(i).x+ "," +verticeslistwithedges.get(i).y+","+verticeslistwithedges.get(i).NodeNo+".");
				                   output.println();
				                   }
				                   
				                   
				                   int numberOfVerticeswithoutedges = verticeslistwithoutedges.size();
				                   output.println("no of vertices present in the graph without edges are:"+numberOfVerticeswithoutedges+".");
					                   output.print("Vertices list with out edges are:");
					                   for(int i=0;i<verticeslistwithoutedges.size();i++)
					                   {
					                   output.print(verticeslistwithoutedges.get(i).name+ "," +verticeslistwithoutedges.get(i).x+ "," +verticeslistwithoutedges.get(i).y+","+verticeslistwithoutedges.get(i).NodeNo+".");
					                   output.println();
					                   }
					                   
			                   
			                   
			                   
			                   int numberOfEdges = edgeslist.size();
			                   output.println("no of edges  present in the graph are:"+numberOfEdges+".");
			                   output.print("The Edges are:");
			                 
		                   for(int i=0;i<edgeslist.size();i++)
		                   {
		                   output.print(edgeslist.get(i).u+ "," +edgeslist.get(i).v+ "," +edgeslist.get(i).weight+".");
		                   output.println();
		                   }
		                  
		                   output.close();
		                   chooser.setSelectedFile(f);
				        }      catch (IOException e1) {
				        }
				    
	               JOptionPane.showMessageDialog(export, "Exporting Succesfully....");
	               
	             //  }
	          }
	      }); 
	    
	    
	    
	    setTitle("Finding Shortest path");
	  //Set default close operation for JFrame  
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
	     
	    //Set initial JFrame size to :  
	    //WIDTH = 800 pixels  
	    //HEIGHT = 900 pixels  
	    setSize(800,700);  
	     
	    //Make JFrame visible  
	    setVisible(true);  
	    
	    
	  
	}
	
	public  class Vertex implements Displayable {
		    private int x, y;
		    private String name;
		    public int NodeNo;
		    Vertex(String name, int x, int y,int nodeno) {
		      this.name = name;
		      this.x = x;
		      this.y = y;
		      this.NodeNo = nodeno;
		    }
		    
		    public int getX() {
		      return x;
		    }
		    public void setX(int i){
		    	x=i;
		    }
		    
		    public int getY() {
		      return y;
		    }
		    public void setY(int i){
		    	y=i;
		    }
		    
		    public String getName() {
		      return name;
		    }
		    
		    public boolean equals(Object o) {
		      return ((Vertex)o).name.equals(this.name);
		    }
		  }
		  
		  class GraphView extends javax.swing.JPanel {
			  
			  
			  boolean printShortesrtpath = false;	  
			  java.util.List<Integer> path1;
		    Graph<? extends Displayable> graph;
		    List<? extends Displayable> path;
		    List<Vertex> vertices1;
		    private AbstractGraph<? extends Displayable>.Tree tree;
		    
		    public GraphView(Graph<? extends Displayable> graph, 
		        List<? extends Displayable> path) {
		      this.graph = graph;
		      this.path = path;
		    }
		    
		    public GraphView(Graph<? extends Displayable> graph) {
		      this.graph = graph;
		    }

		    public void setTree(AbstractGraph<? extends Displayable>.Tree tree) {
		      this.tree = tree;
		      repaint();
		    }
		    public void setTree1(boolean flag,java.util.List<Integer> path,List<Vertex> vertices) {
		    	path1= path;
		    	vertices1 = vertices;
		    	printShortesrtpath = flag;
		      }

		    public void setPath(List<? extends Displayable> path) {
		      this.path = path;
		      repaint();
		    }
		    
		    protected void paintComponent(java.awt.Graphics g) {
		      super.paintComponent(g);
		      
		      // Draw vertices
		      List<? extends Displayable> vertices = graph.getVertices();
		      
		      for (int i = 0; i < graph.getSize(); i++) {
		        int x = vertices.get(i).getX();
		        int y = vertices.get(i).getY();
		        String name = vertices.get(i).getName();
		        
		        g.fillOval(x - 8, y - 8, 16, 16);
		        g.drawString(name, x - 12, y - 12);
		      }
		      
		    
		      
		      //drawing edges
		      for (int i = 0; i <edgeslist.size(); i++)
		      {
		    	  int stVertex = edgeslist.get(i).u;
		    	  int endVertex = edgeslist.get(i).v;
		    	  
		          int x1 =verticeslist.get(stVertex).x;
		          int y1 = verticeslist.get(stVertex).y;
		          int x2 = verticeslist.get(endVertex).x;
		          int y2 =  verticeslist.get(endVertex).y;
		          
		          g.drawLine(x1, y1, x2, y2);
		       
		       }
		      
		      
		    //drawing weights
		      for (int i = 0; i <edgeslist.size(); i++)
		      {
		        	
		    	  int stVertex = edgeslist.get(i).u;
		    	  int endVertex = edgeslist.get(i).v;
		    	  
		    			  if(stVertex< endVertex)
		    			  {
		        
		          int x1 =verticeslist.get(stVertex).x;
		          int y1 = verticeslist.get(stVertex).y;
		          int x2 = verticeslist.get(endVertex).x;
		          int y2 =  verticeslist.get(endVertex).y;
		          g.drawString(edgeslist.get(i).weight + "", (x1 + x2) / 2, (y1 + y2) / 2 - 6);
		     
		    			  }
		       }
		      
		      if(printShortesrtpath)
		      {
		       if (path1!=null) {      
		        g.setColor(java.awt.Color.RED);
		        for (int i = 0; i < path1.size()-1; i++) {
		         
		            int x1 = vertices1.get(path1.get(i)).getX();
		            int y1 = vertices1.get(path1.get(i)).getY();
		            int x2 = vertices1.get(path1.get(i+1)).getX();
		            int y2 = vertices1.get(path1.get(i+1)).getY();
		            
		            g.drawLine(x1, y1, x2, y2);  
		          }
		        
		      }
		      }
		      // Highlight the edges in the spanning tree
		      if (tree != null) {      
		        g.setColor(java.awt.Color.RED);
		        for (int i = 0; i < tree.getNumberOfVerticesFound(); i++) {
		          if (tree.getParent(i) != -1) {
		            int v = tree.getParent(i);
		            int x1 = graph.getVertex(i).getX();
		            int y1 = graph.getVertex(i).getY();
		            int x2 = graph.getVertex(v).getX();
		            int y2 = graph.getVertex(v).getY();
		            
		            g.drawLine(x1, y1, x2, y2);  
		          }
		        }
		      }
		        
		      // Display the path
		      if (path != null) {
		        g.setColor(Color.RED);
		        for (int i = 1; i < path.size(); i++) {
		          int x1 = path.get(i).getX();
		          int y1 = path.get(i).getY();
		          int x2 = path.get(i - 1).getX();
		          int y2 = path.get(i - 1).getY();
		          g.drawLine(x1, y1, x2, y2);
		        }
		      }
		    }
		    
		    public Dimension getPreferredSize() {
		      return new Dimension(700, 500);
		    }
		  }
		  public void mouseClicked(MouseEvent me){
				
				
			}
				
			
			public void mouseEntered(MouseEvent evtObj){
				
			}
			public void mouseDragged(MouseEvent me) {
				
				// TODO Auto-generated method stub
				//String name=me.
				
			}
			@Override
			public void mouseMoved(MouseEvent me) {
				
				
				// TODO Auto-generated method stub
				
			}
			@Override
			public void mouseExited(MouseEvent arg0){
				// TODO Auto-generated method stub
				
			}
			@Override
			public void mousePressed(MouseEvent me) 
			{
				for(int i=0;i<verticeslist.size();i++)
				{
					if((me.getX()>(verticeslist.get(i).getX()-8))&&(me.getX()<(verticeslist.get(i).getX()+8)))
					{
						if((me.getY()>(verticeslist.get(i).getY())-8)&&(me.getY()<(verticeslist.get(i).getY()+8)))
						{
							z=i;
						}
						
					}
			
				}
				
			repaint();
				// TODO Auto-generated method stub
				
			}
			
			
			public void mouseReleased(MouseEvent me) {
				if(z<verticeslistwithedges.size()){
					verticeslistwithedges.get(z).setX(me.getX());
					verticeslistwithedges.get(z).setY(me.getY());
				}
				
				verticeslist.get(z).setX(me.getX());
				verticeslist.get(z).setY(me.getY());
				
				
				
				
			 repaint();
			}
		  
		  

		  
		  public static void main(String[] args) {
			  
			    
			    GuiForShortestPath applet = new GuiForShortestPath();
			    
			    
			    
		  }

}
